package docx;

import java.io.File;
import java.io.IOException;


import javax.xml.bind.JAXBException;

import org.apache.commons.io.FileUtils;

import org.docx4j.convert.in.xhtml.XHTMLImporterImpl;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart;

public class xhtml2docx {

	
	public static void main(String[] args) throws IOException, JAXBException, Docx4JException {
		String dir = System.getProperty("user.dir") + "/sample-docs/docx/";
		String htmlFilePath = dir + "/DocxToXhtml.html";
		
		// XHTML to docx
        String stringFromFile = FileUtils.readFileToString(new File(htmlFilePath), "UTF-8");
		
        
		WordprocessingMLPackage docxOut = WordprocessingMLPackage.createPackage();
		NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
		docxOut.getMainDocumentPart().addTargetPart(ndp);
		ndp.unmarshalDefaultNumbering();	
		

		XHTMLImporterImpl xHTMLImporter = new XHTMLImporterImpl(docxOut);
		xHTMLImporter.setHyperlinkStyle("Hyperlink");
					
		docxOut.getMainDocumentPart().getContent().addAll(xHTMLImporter.convert(stringFromFile, null) );
				
		docxOut.save(new java.io.File(dir + "/DocxToXhtmlAndBack.docx") );

	}

}
